package com.coincalf.wallet.blockchain.client;

import com.coincalf.feign.CoinServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class TrxUrlClient {

    @Autowired
    private CoinServiceClient coinServiceClient;

    private static CoinServiceClient coinClient;

    @PostConstruct
    public void init() {
        TrxUrlClient.coinClient = coinServiceClient;
    }

    private String rpcServerUrl;

    public String getRpcServerUrl() {
        return rpcServerUrl;
    }

    private void setRpcServerUrl(String rpcServerUrl) {
        this.rpcServerUrl = rpcServerUrl;
    }

    private TrxUrlClient() {
    }

    private static volatile TrxUrlClient trxClient = null;

    public static TrxUrlClient getTrxCLient() {
        if (trxClient == null) {
            synchronized (TrxUrlClient.class) {
                if (trxClient == null) {
                    try {
                        trxClient = new TrxUrlClient();
                        trxClient.setRpcServerUrl(coinClient.selectRpcServerUrl("TRX"));
                    } catch (Exception e) {
                        log.warn("获取TRX CLIENT ERROR e={}", e);
                        e.printStackTrace();
                    }
                }
            }
        }
        return trxClient;
    }

}
